//var	rotots_status=0;/*robot status 0:sleep;1:wakup;2:running*/
var	conect_count=0;
var   server_url="http://sluojin.herokuapp.com:80";
//var 	 ourID="";
var	wakup_ok=0;/*0:idle or sleeping; 1:wait wakup; 2:wakup success;
				    3: wait hang up*/
function _UISetting()
{
	this.myID="" ; 
	this.password="" ; 
	this.volume=0.0; 
	this.recent_call=""; 

}
var UI_set;
/*
window.alert = function( words){
	document.getElementById("ialert_txt").innerText=words;
	window.location.hash = "ialert";
//	$( "#ialert" ).dialog();
	
}*/
function data_cmd(send_string)
{
	
//  alert(send_string);
	//window.CallJave.clickOnAndroid(send_buf,send_string.length);
	//window.call_java.JScallJava('data',send_string);// 
	//window.call_java.JScallJava('bluetooth',send_string);
	BT_car_speed(send_string);
}

function test_click()
{
	var ss=document.getElementById('logtest1id').value;
	//var ss=$("#logtest1").val();
	//alert(ss);
	window.call_java.JScallJava('net',ss);
}
function login()
{
	window.call_java.JScallJava('net','a;login');
}

function setID()
{
	var dd="";
	UI_set.myID=document.getElementById('caller-id').value;
	UI_set.password=document.getElementById('caller-id-pass').value;
	if(UI_set.password.length>0)
		dd= UI_set.myID+":"+UI_set.password ;

	// alert(dd);
	
	Set_settings();
	window.call_java.JScallJava('restart:','');
	//setInterval(function(){location.reload();}, 1000);
	
}

function login_in(myid)
{
	//alert(idmy+';login');
	var cmd_logi =myid+";login";
	window.call_java.JScallJava('net',cmd_logi);
}
function connect_and_login()
{
	if(UI_set.myID.length>0)
	{
		var logins="login_in(\'"+UI_set.myID+"\');";
		setTimeout(logins,2000);
		window.call_java.JScallJava('net',server_url);
	}

}
function car_cmd(send_string)
{
//	document.querySelector('#s_status_q').innerText ="car send:"+ send_string;
 	send_string=send_string+";\r\n";
	conn.send(send_string);
}

function update_user()
{
	window.call_java.JScallJava('net',UI_set.myID+';get live');
	add_user("");


}


function read_obj(receive_str,obj_name,end_str)
{
	var tmp_name,get_obj='';
	var j;
	var i=receive_str.indexOf(obj_name);
	if(i>=0)
	{
		tmp_name=receive_str.substr(i+obj_name.length);		
		j=tmp_name.indexOf(end_str);
		if(j>0)
			get_obj=tmp_name.substr(0,j);
		else
			get_obj=tmp_name.substr(0);
	}
	return get_obj;
}

function pase_setting(receive_str)
{
	var i=0;
	UI_set.myID=read_obj(receive_str,"myID:",";");
	if(UI_set.myID.length>0)
	{
		i=UI_set.myID.indexOf(":");
		if(i>0)
		{
			document.querySelector('#caller-id-pass').value = UI_set.myID.substr(i+1);	 
			UI_set.password=UI_set.myID.substr(i+1);
			UI_set.myID=UI_set.myID.substr(0,i);
			
		}
		document.querySelector('#caller-id').value = UI_set.myID;	 
		document.querySelector('#myidd').innerText ="MyID:"+UI_set.myID;
	}
	//////
	UI_set.volume=read_obj(receive_str,"audio:",";");
	UI_set.recent_call=read_obj(receive_str,"recent call:",";");

//	alert("set: "+UI_set.myID+"_"+UI_set.password+"_"+UI_set.volume+"_"+UI_set.recent_call);
	disconnect=1;
}

function Set_settings()
{
	var set_tmp="";
	var element = document.getElementById("remote-video");
			
	UI_set.volume=""+element.volume;
	if(UI_set.password.length>0)
		set_tmp='myID:'+UI_set.myID+':'+UI_set.password+';audio:'+UI_set.volume+';recent call:'+UI_set.recent_call+';';
	else
		set_tmp='myID:'+UI_set.myID+';audio:'+UI_set.volume+';recent call:'+UI_set.recent_call+';';
//	alert(set_tmp);
	window.call_java.JScallJava('setting:',set_tmp);
}

function JavacallJS(receive_str)
{
	
	//alert("receive_str:"+receive_str);
	document.querySelector('#s_status').innerText ="usr server:"+ receive_str;
	document.querySelector('#s_status0_0').innerText ="usr server:"+ receive_str;
	var i=receive_str.indexOf("setting:");
	if(i==0)
	{
		pase_setting(receive_str);
	}
/*	var i=receive_str.indexOf("myID:");
	if(i==0)
	{
		
		ourID=receive_str.substr(i+5);
		i=ourID.indexOf(":");
		if(i>0)
		{
			document.querySelector('#caller-id-pass').value = ourID.substr(i+1);	 
			ourID=ourID.substr(0,i);
		}
		document.querySelector('#caller-id').value = ourID;	 
	
		document.querySelector('#myidd').innerText ="MyID:"+ourID;
		disconnect=1;
		//connect_and_login();
		return; 
	}
	*/
	i=receive_str.indexOf(";call;");
	if(i>0)
	{
		var net_string=receive_str.substr(i+6)+";answer;"+receive_str.substr(0,i);
	//	alert(net_string);
	 	window.call_java.JScallJava('net',net_string);
		return;
	}
	i=receive_str.indexOf(";answer;");
	if(i>0)
	{
		dial_toID=receive_str.substr(0,i);
		if(wakup_ok==1)
		{
			wakup_ok=2;
			event_java=1;
			$('#remote-video').addClass('loading');
		}
		else if(wakup_ok==3)
		{
			wakup_ok=0;
		//	event_java=0;
			alert("hang up success");
		}
		
		return;
	}
	i=receive_str.indexOf("hi sheep");
	if(i==0)
	{
		conect_count=0;
	}
	i=receive_str.indexOf("hi new sheep");
	if(i==0)
	{
		conect_count=0;
		$('#s_statusgif').removeClass('loading');
	}
	
	i=receive_str.indexOf(";alive");
	if(i>0)
	{
		var liveid=receive_str.substr(0,i);
		add_user(liveid);
		/*if(liveid==UI_set.recent_call)
		{
			$("#day_id option").filter(function() {
				    return this.text == liveid; 
				}).attr('selected', true);
		}*/
	}
	i=receive_str.indexOf(";BTalive");
	if(i>0)
	{
		var liveid=receive_str.substr(0,i);
		add_user_BT(liveid);
	}
	i=receive_str.indexOf("voice_error:");
	if(i==0)
	{
		document.querySelector('#s_status0_0').innerText ="voice_error:"+ receive_str;
	}
	i=receive_str.indexOf("voice_result:");
	if(i==0)
	{
		document.querySelector('#s_status0_0').innerText ="voice_result:"+ receive_str;

		voice_control(receive_str.substr(13));
	}

}

function hangup()
{
	
	wakup_ok=3;
	event_java=0;
	wakup_call_t();
	//window.call_java.JScallJava('net',document.querySelector('#caller-id').value+';call;'+UI_set.recent_call);

}

function wakup_call_t()
{
 /*	if(($('#day_id').val()=="Call user")||($('#day_id').val()==null))
 	{
		alert(" please choose a remote user");
		return;
 	}
	 */
	UI_set.recent_call=current_call;//$('#day_id').val();
	
	if(wakup_ok==2)
	{
		event_java=1;
		return;
	}
	else if(wakup_ok==0) 
	{
		wakup_ok=1;
		event_java=1;
		setTimeout("if(wakup_ok!=2) alert('Time out!');",30000);
	}
	else if(wakup_ok==3)
	{
		setTimeout("if(wakup_ok!=0) alert('Time out!');",10000);
		window.call_java.JScallJava('net',document.querySelector('#caller-id').value+';hang up;'+UI_set.recent_call);
		return;
	}
	//window.call_java.JScallJava('net',document.querySelector('#caller-id').value+';call;'+document.querySelector('#recipient-id').value);
	window.call_java.JScallJava('net',document.querySelector('#caller-id').value+';call;'+UI_set.recent_call);
	Set_settings();	
 
	
	
}
/*
function timeout_stop()
{
	var t=setTimeout("car_stop();",500);
}
*/
function timeout_stop(times)
{
	var t=setTimeout("car_stop();",times);
}

function car_run()
{
	//alert("car run:");
	car_cmd("b1");
	
	 timeout_stop(500);
	
}
function car_runhi()
{
	car_cmd("b1");
	
	 timeout_stop(500);
	
}
function car_left()
{
	 
	car_cmd("r1");
	car_cmd("l-1");
	
	timeout_stop(300);
}
function car_right()
{
	car_cmd("r-1");
	car_cmd("l1");
	
	timeout_stop(300);
}
function car_back()
{
//	alert(car_back);
	car_cmd("b-1");
	car_cmd("r-1");
	car_cmd("l-1");
	
	
	//car_cmd("r-4000");
	//car_cmd("l-4000");
	//setTimeout("car_cmd('l-4000'); ",100);
	 timeout_stop(500);
}

function car_stop()
{

	car_cmd("b0");
	car_cmd("b0");
	
}

var led_on_off=0;
function led_onoff(scmd)
{
	if(led_on_off==0)
		led_on_off=1;
	else
		led_on_off=0;
	if(scmd=="local")
	{
		//alert("d"+led_on_off);
		Car_control("d"+led_on_off);
	}
	else
		car_cmd("d"+led_on_off);
		
}

function x_send()
{
	var sendv="x"+document.getElementById("pointsx").value;//document.querySelector('#net_send-id').value;
	car_cmd(sendv);
}
function y_send()
{
	var sendv="y"+document.getElementById("pointsy").value;//document.querySelector('#net_send-id').value;
	car_cmd(sendv);
}
function net_send()
{

	var sendv=document.getElementById("net_sendid").value;//document.querySelector('#net_send-id').value;
	alert(sendv);
	car_cmd(sendv);
}

function open_port()
{

	window.call_java.JScallJava('uart:9600',0);
}


function BT_car_speed(send_string)
{
	send_string=send_string+";\r\n";
	//alert(send_string);
	window.call_java.JScallJava('bluetooth',send_string);
}
function initialize()
{
	robot_init();
	 SQUARIFIC.touchControlDemo = 
	 	new SQUARIFIC.TouchControlDemo(document.getElementById('BT_control'), 'touchControlDemo', {});
	 BT_auto_conect=0;
	$("#homeb").click(function(){
	    $('#boxp').animate({'bottom':'0'},300);
	});
	$('#closeb').click(function(){
	    $('#boxp').animate({'bottom':'-100%'},300)        
	});	
	//document.querySelector('#s_status').innerText = "usr server:"+'1234';
	$('#s_statusgif').addClass('loading');
	setInterval(function(){sheepTimer()}, 1000);
	document.querySelector('#myidd').innerText ="MyID";
	
	var vid = document.getElementById("remote-video");
	//document.getElementById("local-video").style.webkitTransform = "rotate(-90deg)";
	vid.oncanplaythrough = function() {
		event_java=0;
		$(this).volume =parseFloat(UI_set.volume);// 0.2;
		
		$(this).removeClass('background');
		//$(this).removeAttr("poster");
		$(this).removeClass('loading');
		///$('#local-video').removeClass('background');
	//	document.getElementsByTagName("remote-video").setAttribute('controls', 'true');
		//document.querySelector('#s_status').innerText ="usr server:"+ receive_str;
	};
	
	  
/*	$('#remote-video').on('loadstart', function (event) {
		//$(this).attr("poster", "loading.gif");
		
		 $('#remote-video').addClass('loading');
		alert("load start");
	});*/

 
	window.call_java.JScallJava('net','loaded');

	//open_port();
	//setTimeout("window.call_java.JScallJava('data','b6;'); ",1000);
	//setTimeout("window.call_java.JScallJava('data','b0;');",1200);
	setTimeout("update_user();",2000);

	$('#remote-video').addClass('background');
	
}


function sheepTimer()
{
	//1s
	conect_count++;
	if(conect_count>2)
	{
		conect_count=0;
		 
		//if(document.getElementById("day_id").length<2)
		if(on_user.length==0)
			 update_user();

		
		var vid= document.getElementById("remote-video");
		if(vid=="muted")
			vid.volume=0.0;
		else
			vid.volume=1.0;

	}

}



function goto_mode_page(type)
{

	mode=type;//"internet";
	if(mode=="internet")
	{
		location.hash ="#page0";
		
		//location.hash ="#pagemode";
		//document.getElementById("master_img").src="images/master_remote.jpg"
		//document.getElementById("slave_img").src="images/slave_remote.jpg"

	}
	else if(mode=="local")
	{
		//mode="local";
		location.hash ="#pagemode";
		document.getElementById("master_img").src="images/masterp2p.jpg"
		document.getElementById("slave_img").src="images/slavep2p.jpg"
		
	}
}

